package com.xnyzc.lhy.mis.service.impl.cms.invoice;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.mis.entity.dictionaries.OaSysDict;
import com.xnyzc.lhy.mis.entity.order.OaCOrderPersonal;
import com.xnyzc.lhy.mis.entity.result.cms.invoice.OaSysInvoiceDidiApplyResults;
import com.xnyzc.lhy.mis.mapper.cms.invoice.OaSysInvoiceDidiApplyMapper;
import com.xnyzc.lhy.mis.mapper.dictionaries.OaSysDictMapper;
import com.xnyzc.lhy.mis.mapper.order.OaCOrderPersonalMapper;
import com.xnyzc.lhy.mis.service.cms.invoice.MisOaSysInvoiceDidiApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 发票接口实现类
 * @author zhaolijie
 * @date
 */
@Service
public class MisOaSysInvoiceDidiApplyServiceImpl implements MisOaSysInvoiceDidiApplyService {

    @Autowired
    private OaSysInvoiceDidiApplyMapper oaSysInvoiceDidiApplyMapper;

    @Autowired
    private OaSysDictMapper oaSysDictMapper;

    @Autowired
    private OaCOrderPersonalMapper oaCOrderPersonalMapper;

    /**
     * 分页获取发票列表
     * @param oaSysInvoiceDidiApplyResults
     * @return
     */
    @Override
    public Rv getInvoiceList(OaSysInvoiceDidiApplyResults oaSysInvoiceDidiApplyResults) {
        if (CheckUtil.objIsEmpty(oaSysInvoiceDidiApplyResults.getCurrent(),oaSysInvoiceDidiApplyResults.getSize())) {
            throw PangException.create(EErrorCode.missingArg);
        }
        IPage<OaSysInvoiceDidiApplyResults> invoiceList = oaSysInvoiceDidiApplyMapper.getInvoiceList(new Page(oaSysInvoiceDidiApplyResults.getCurrent(), oaSysInvoiceDidiApplyResults.getSize()), oaSysInvoiceDidiApplyResults);
        List<OaSysInvoiceDidiApplyResults> records = invoiceList.getRecords();
        if (records.size()>0) {
            for (int i = 0; i < records.size(); i++) {
                String invoiceOrderIds = records.get(i).getInvoiceOrderIds();
                if (CheckUtil.strIsNotEmpty(invoiceOrderIds) && true == invoiceOrderIds.contains(",")){
                    String[] split = invoiceOrderIds.split(",");
                    records.get(i).setChooseOrderId(split[0]);
                    List<String> orders = new ArrayList<>();
                    for (int j = 0; j < split.length; j++) {
                        orders.add(split[j]);
                    }
                    records.get(i).setOrderIds(orders);
                }
            }
        }
        invoiceList.setRecords(records);
        return  Rv.wrap(invoiceList);
    }

    /**
     *  获取发票状态字典信息
     * @return
     */
    @Override
    public Rv getInvoiceSysDict() {
        QueryWrapper<OaSysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaSysDict.TYPE,"didi_invoice_type");
        List<OaSysDict> oaSysDicts = oaSysDictMapper.selectList(queryWrapper);
        return Rv.wrap(oaSysDicts);
    }

    /**
     * 导出发票列表信息
     * @param oaSysInvoiceDidiApplyResults
     * @return
     */
    @Override
    public Rv exportGetInvoiceList(OaSysInvoiceDidiApplyResults oaSysInvoiceDidiApplyResults) {
        String receiveName = oaSysInvoiceDidiApplyResults.getReceiveName();
        String receiveMobile = oaSysInvoiceDidiApplyResults.getReceiveMobile();
        String didiInvoiceStatus = oaSysInvoiceDidiApplyResults.getDidiInvoiceStatus();
        Date beginTime = oaSysInvoiceDidiApplyResults.getBeginTime();
        Date overTime = oaSysInvoiceDidiApplyResults.getOverTime();
        List<OaSysInvoiceDidiApplyResults> exportGetInvoiceList = oaSysInvoiceDidiApplyMapper.exportGetInvoiceList(receiveName, receiveMobile, didiInvoiceStatus, beginTime, overTime);
        return Rv.wrap(exportGetInvoiceList);
    }

    /**
     * 获取发票详情信息
     * @return
     */
    @Override
    public Rv getInvoiceItem(Long recordId) {
        if (CheckUtil.objIsEmpty(recordId)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        OaSysInvoiceDidiApplyResults invoiceItem = oaSysInvoiceDidiApplyMapper.getInvoiceItem(recordId);
        String invoiceOrderIds = invoiceItem.getInvoiceOrderIds();
        List<String> orders = new ArrayList<>();
        if (CheckUtil.strIsNotEmpty(invoiceOrderIds) && true == invoiceOrderIds.contains(",")){
            String[] split = invoiceOrderIds.split(",");
            for (int j = 0; j < split.length; j++) {
                orders.add(split[j]);
            }
        } else if (CheckUtil.strIsNotEmpty(invoiceOrderIds) && false == invoiceOrderIds.contains(",")){
                orders.add(invoiceOrderIds);
        }
        QueryWrapper<OaCOrderPersonal> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(OaCOrderPersonal.ORDER_NO,orders);
        List<OaCOrderPersonal> oaCOrderPersonals = oaCOrderPersonalMapper.selectList(queryWrapper);
        invoiceItem.setOrderCount(oaCOrderPersonals.size());
        BigDecimal orderCountMoney = invoiceItem.getOrderCountMoney();
        for (int i = 0; i < oaCOrderPersonals.size(); i++) {
            orderCountMoney = orderCountMoney.add(oaCOrderPersonals.get(i).getActualFee());
        }
        invoiceItem.setOrderCountMoney(orderCountMoney);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("invoiceItem",invoiceItem);
        jsonObject.put("oaCOrderPersonals",oaCOrderPersonals);
        return Rv.wrap(jsonObject);
    }
}
